
output := test.bin

CC := gcc
#ifdef ARM_LINUX_GCC
STRIP := arm-linux-strip
#else
STRIP := strip
#endif

all: $(output)

# include dirs
includir := $(shell find -type d)
excludir := $(shell find  -type d | grep ".git")
excludir += $(shell find  -type d | grep ".svn")
includir := $(filter-out $(excludir),$(includir))
includir := $(addprefix -I,$(includir))

# make deps
sources := $(shell find -name "*.c")
sources += __all_files__.c
objects := $(sources:.c=.o)
depends := $(sources:.c=.d)

# flags and libs
CPPFLAGS += $(dirs) 
CPPFLAGS += -Wall -Wextra -g 
LIBS += -lrt -lpthread -lzlog

# xs-lib objects for all 
com_objs := $(shell find ./xs-lib -name "*.c")
com_objs += __all_files__.c
com_objs := $(com_objs:.c=.o)

# add your other obj dir
src_objs := $(shell find ./src -name "*.c")
src_objs += __all_files__.c
src_objs := $(src_objs:.c=.o)

#
#add by tsihang 05-20-2013 for threadpool
#thread_pool_objs := $(shell find ./thread_pool -name "*.c")
#thread_pool_objs += __all_files__.c
#thread_pool_objs := $(thread_pool_objs:.c=.o)
#
#
ifneq ($(MAKECMDGOALS),clean) 
-include $(depends)
endif

test.bin: $(com_objs) $(src_objs) $(thread_pool_objs)
	$(CC) -o $@ $^  -lpthread -lrt #-lzlog 
	$(STRIP) test.bin

# for 
allfiles := $(shell find -name "*.c" -o -name "*.h" -o -name "*.cpp"| grep -v "__all_files__.c")
allfile_deps := $(allfiles)
allfiles := $(sort $(allfiles))
allfiles := $(subst ./,,$(allfiles))
allfiles := $(addprefix \",$(allfiles))
allfiles := $(allfiles:=\",)

__all_files__.c: $(allfile_deps) Makefile
	@echo '#ifdef __cplusplus' > $@
	@echo 'extern "C"{' >> $@
	@echo '#endif' >> $@
	@echo 'const char* g_mem_files[] = {' >> $@
	@for filename in $(allfiles); \
		do \
		echo $$filename >> $@; \
		done
	@echo '};' >> $@
	@echo 'int g_mem_file_count=' >> $@
	@echo $(words $(allfiles)) >> $@
	@echo ';' >> $@
	@echo '#ifdef __cplusplus' >> $@
	@echo '}' >> $@
	@echo '#endif' >> $@

# all files end

CPPFLAGS += $(includir)
CPPFLAGS += -g -Wall -Wextra 

%.d: %.c
	@set -e; \
       $(CC) -MM $(CPPFLAGS) $< > $@; \
       sed -i 's#.*\.o#$@#' $@;  \
       sed -i 's#\.d#\.o#' $@;  \

.PHONY: clean
clean:
	rm $(objects) $(depends) *.log *.bin *.so *.d *.o *.tmp *.a -rf 

.PHONY: install
install:

.PHONY: uninstall
uninstall:

